package cc.cary.warehouse.service.impl;

import cc.cary.warehouse.common.R;
import cc.cary.warehouse.dto.UserDTO;
import cc.cary.warehouse.entity.User;
import cc.cary.warehouse.form.UserQueryForm;
import cc.cary.warehouse.helper.PageHelper;
import cc.cary.warehouse.mapper.UserMapper;
import cc.cary.warehouse.service.IUserService;
import cc.cary.warehouse.untils.Beans;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public User findByName(String name) {
        return baseMapper.findByName(name);
    }

    @Override
    public R<IPage<UserDTO>> pages(UserQueryForm form) {
        IPage<User> userPage = this.lambdaQuery()
                .like(Beans.strNotEmpty(form.getName()), User::getName, form.getName())
                .page(new Page<>(form.getPage(), form.getPageSize()));
        IPage<UserDTO> page = PageHelper.coverTo(userPage, UserDTO.class);
        return R.ok(page);
    }
}
